package com.demo.day419;

import java.util.Arrays;

public class pra {
    /**
     * @Auther: 86176
     * @Date: 2021/4/19 - 04 - 19 - 20:42
     * @Description: com.demo.day419
     * @version: 1.0
     * 
     */
    public static void f1() {
        int o = 5;
        int[][] arr = new int[o][o];
        int step = 1;//1 往右 2 往下 3 往左 4 往上;
        int x = 0, y = 0;
        int f = 0, x1 = 0, y1 = 1, x2 = 1, y2 = 1;
        for (int i = 1; i <= arr.length * arr.length; ) {
            switch (step) {
                case 1:
                    if (f == o - x1) {
                        step = 2;
                        x1 += 2;
                        y++;
                        x--;
                        f = 0;
                    } else {
                        arr[y][x] = i;
                        x++;
                        i++;
                        f++;
                    }
                    break;


                case 2:
                    if (f == o - y1) {
                        step = 3;
                        y1 += 2;
                        x--;
                        y--;
                        f = 1;
                    } else {
                        arr[y][x] = i;
                        y++;
                        i++;
                        f++;
                    }
                    break;


                case 3:
                    if (f == o + 1 - x2) {
                        step = 4;
                        x2 += 2;
                        y--;
                        x++;
                        f = 1;
                    } else {
                        arr[y][x] = i;
                        x--;
                        i++;
                        f++;
                    }
                    break;

                case 4:
                    if (f == o - y2) {
                        step = 1;
                        y2 += 2;
                        x++;
                        y++;
                        f = 0;
                    } else {
                        arr[y][x] = i;
                        y--;
                        i++;
                        f++;
                    }
                    break;

            }

        }
        for (int j = 0; j < arr.length; j++) {
            System.out.println(Arrays.toString(arr[j]));
        }


    }

    public static void main(String[] args) {
        f1();
    }
}
